package com.tct.worktimeboard.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * map工具类
 **/
@Component
public class MapUtil {
    /**
     * 根据值获取key方法
     */
    public Integer getKeyByValue(Map<Integer,String> map, String value){
        for (Map.Entry<Integer, String> entry : map.entrySet()) {
            if (Objects.equals(entry.getValue(), value)) {
                return entry.getKey();
            }
        }
        return null;
    }

    /**
     * 用于判断指定map中指定位置是否有值
     */
    public Boolean isData(Map<Integer,String> map){
        if(map.get(0) != null && !map.get(0).equals("删除")){
            return true;
        }
        return false;
    }

    /**
     * 根据集合中指定的元素去重
     * titleList = titleList.stream().filter(distinctByKey(b -> b.get("id"))).collect(Collectors.toList());
     * @param keyExtractor
     * @param <T>
     * @return
     */
    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        System.out.println("这个函数将应用到每一个item");
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }
}
